package com.embracesource.business.util;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 通用返回数据类
 * </p>
 *
 * @author gnerv
 * @since 2018-06-28
 */
public class R extends HashMap<String, Object> {

    private static final long serialVersionUID = 1L;

    /**
     * 200 请求处理成功.
     */
    public static final String[] OK_200 = {"200", "成功"};
    /**
     * 请求数据有错误
     */
    public static final String[] OK_400 = {"400", "请求数据有错误"};
    /**
     * 无访问权限
     */
    public static final String[] OK_401 = {"401", "无访问权限"};
    /**
     * 访问数据不存在
     */
    public static final String[] OK_404 = {"404", "访问数据不存在"};
    /**
     * 500 请求处理失败.
     */
    public static final String[] ERROR_500 = {"500", "失败"};

    private Map<String, Object> data = new HashMap<>();

    public R() {
        put("code", 200);
        put("message", "");
        put("data", data);
    }

    public R(String[] str) {
        put("code", str[0]);
        put("message", str[1]);
        put("data", data);
    }

    public R(String code, String message) {
        put("code", code);
        put("message", message);
        put("data", data);
    }

    public static R ok() {
        R r = new R();
        return r;
    }

    public static R ok(String[] str) {
        R r = new R(str);
        return r;
    }

    public static R ok(String code, String message) {
        R r = new R(code, message);
        return r;
    }

    public static R error(String str) {
        R r = new R();
        r.put("code", 500);
        r.put("message", str);
        return r;
    }

    public static R error(String[] str) {
        R r = new R(str);
        return r;
    }

    public static R error(String code, String message) {
        R r = new R(code, message);
        return r;
    }

    public R data(String key, Object value) {
        data.put(key, value);
        return this;
    }

    public R data(Map<String, Object> map) {
        data.putAll(map);
        return this;
    }
}